package com.bss.command;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//import org.apache.catalina.connector.Request;

import com.bss.dao.RoomDao;
import com.bss.beans.Room;
import com.bss.beans.Dom;
import com.bss.beans.RoomAndDomDetails;
import com.bss.beans.RoomAllocation;
import com.bss.dao.CheckInOutDao;

public class RoomCommand extends AbstractCommand {

	@Override
	public boolean execute(HttpServletRequest request, HttpSession session)
			throws Exception {
		// TODO Auto-generated method stub
		
		if(Integer.parseInt((session.getAttribute("action")).toString())==1)
		{
			 if(addroom(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==2)
		{
			 if(addbed(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==3)
		{
			 if(allocateRoomDom(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==4)
		{
			 if(deallocateRoomDom(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==5)
		{
			 if(temporaryCheckOut(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==6)
		{
			 if(temporaryCheckIn(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		
		return false;
	}

	@Override
	public boolean falseforward(HttpServletResponse response,
			HttpSession session) throws Exception {
		// TODO Auto-generated method stub
		
		if(Integer.parseInt((session.getAttribute("action")).toString())==1)
		{
			if(session.getAttribute("Error")==null)
			{
			session.setAttribute("welcomemsg", "UNABLE TO ADD NEW ROOM");
			response.sendRedirect("roomdom.jsp");
			}
			else
			{
				response.sendRedirect("addnewroom.jsp");
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==2)
		{
			if(session.getAttribute("Error")==null)
			{
			session.setAttribute("welcomemsg", "UNABLE TO ADD NEW BED");
			response.sendRedirect("roomdom.jsp");
			}
			else
			{
				response.sendRedirect("addnewbed.jsp");
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==3)
		{
			if(session.getAttribute("Error")==null)
			{
			session.setAttribute("welcomemsg", "UNABLE TO ALLOCATE ROOM/DOM'S TO PATIENT");
			response.sendRedirect("checkinout.jsp");
			}
			else
			{
				response.sendRedirect("CHECKINOUT/bedandroomallocation.jsp");
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==4)
		{
			if(session.getAttribute("Error")==null)
			{
				session.setAttribute("welcomemsg", "UNABLE TO DEALLOCATE ROOM/DOM'S TO PATIENT");
				response.sendRedirect("checkinout.jsp");
			}
			else
			{
				response.sendRedirect("CHECKINOUT/deallocaterooms.jsp");
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==6)
		{
			session.setAttribute("Error", "PATIENT NOT TEMPORARY CHECKED OUT");
			if(session.getAttribute("Error")==null)
			{
				session.setAttribute("welcomemsg", "PATIENT NOT TEMPORARY CHECKED OUT");
				response.sendRedirect("checkinout.jsp");
			}
			else
			{
				response.sendRedirect("CHECKINOUT/temporaryCheckIn.jsp");
			}
		}
		return false;
	}

	@Override
	public boolean trueforward(HttpServletResponse response, HttpSession session)
			throws Exception {
		// TODO Auto-generated method stub
		
		
		if(Integer.parseInt((session.getAttribute("action")).toString())==1)
		{
			session.setAttribute("welcomemsg", "NEW ROOM ADDED");
			response.sendRedirect("roomdom.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==2)
		{
			session.setAttribute("welcomemsg", "NEW BED ADDED");
			response.sendRedirect("roomdom.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==3)
		{
			session.setAttribute("welcomemsg", "SUCESSFULLY ADDED ROOM/DOM'S");
			response.sendRedirect("checkinout.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==4)
		{
			session.setAttribute("welcomemsg", "SUCESSFULLY DEALLOCATED ROOM/DOM'S");
			response.sendRedirect("checkinout.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==5)
		{
			session.setAttribute("welcomemsg", "SUCESSFULLY TEMPORARY CHECKED OUT");
			response.sendRedirect("checkinout.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==6)
		{
			session.setAttribute("welcomemsg", "SUCESSFULLY TEMPORARY CHECKED IN");
			response.sendRedirect("checkinout.jsp");
		}
		return false;
	}

	private boolean addroom(HttpServletRequest request, HttpSession session) throws Exception
	{
		Room room=new Room();
		ArrayList<String> roomList;
		session.removeAttribute("Error");
		String building=request.getParameter("buildingname");
		room.setSRoomNo(request.getParameter("roomno").toString());
		if(building.length()==0)
		{
		room.setSBuildingName(request.getParameter("buildingName1").toString());
		
			roomList=RoomDao.loadRoomNames(room.getSBuildingName());
			for(int i=0;i<roomList.size();i++)
			{
				if(room.getSRoomNo().compareToIgnoreCase(roomList.get(i))==0)
				{
					session.setAttribute("Error","Building"+room.getSBuildingName()+" Already Has the same room Number"+room.getSRoomNo());
					return false;
				}
			}
		
		}
		else
		{
			room.setSBuildingName(building);
		}
		
		room.setIRoomId(RoomDao.FindMaxRoomId()+1);
		room.setSRoomNo(request.getParameter("roomno").toString());
		room.setICharges(Integer.parseInt(request.getParameter("charges")));
		room.setIFloorNo(Integer.parseInt(request.getParameter("floor")));
		String isdom=request.getParameter("isdom").toString();
		if(isdom.compareTo("y")==0)
		{
			room.setBIsDom(true);
		}
		else
		{
			room.setBIsDom(false);
		}
		
		RoomDao.AddNewRoom(room);
		return true;
	}
	
	private boolean addbed(HttpServletRequest request, HttpSession session) throws Exception
	{
		Dom dom=new Dom();
		
		dom.setIRoomId(Integer.parseInt(request.getParameter("roomid")));
		dom.setSDomNo(request.getParameter("bedno"));
		ArrayList<String> roomList=RoomDao.loadDomNames(dom.getIRoomId());
		
		for(int i=0;i<roomList.size();i++)
		{
			if(dom.getSDomNo().compareToIgnoreCase(roomList.get(i))==0)
			{
				session.setAttribute("Error","Room Already Has the same BED Number"+dom.getSDomNo());
				return false;
			}
		}
		
		session.removeAttribute("Error");
		
		dom.setIDomId(RoomDao.FindMaxDomId()+1);
		dom.setICharges(Integer.parseInt(request.getParameter("charges")));
		RoomDao.AddNewBed(dom);
		
		return true;
	}
	
	public static ArrayList<String> loadBuildingNames() throws Exception
	{
		return RoomDao.loadBuildingNames();
	}
	public static ArrayList<String> loadDomRoomNames(String buildingName) throws Exception
	{
		return RoomDao.loadDomRoomNames(buildingName);
	}
	public static ArrayList<RoomAndDomDetails> loadAllDoms() throws Exception
	{
		return RoomDao.loadAllDoms();
	}
	public static ArrayList<Integer> loadOccupiedDomId() throws Exception
	{
		return RoomDao.loadOccupiedDomId();
	}
	public static ArrayList<RoomAndDomDetails> loadAllRooms() throws Exception
	{
		return RoomDao.loadAllRooms();
	}
	public static ArrayList<Integer> loadOccupiedRoomId() throws Exception
	{
		return RoomDao.loadOccupiedRoomId();
	}
	
	
	private boolean allocateRoomDom(HttpServletRequest request, HttpSession session) throws Exception
	{
		int patientId=Integer.parseInt(request.getParameter("patientId").toString());
		
		
		
		int transactionId=CheckInOutDao.findLastTransactionId(patientId);
		
		if(request.getParameter("roomId")!=null )
		{
			if(request.getParameter("roomId").toString().length() > 0)
			{
				RoomAllocation roomAllocation=new RoomAllocation();
				roomAllocation.setITransactionId(transactionId);
				roomAllocation.setIRoomId(Integer.parseInt(request.getParameter("roomId").toString()));
				roomAllocation.setIAmount(Integer.parseInt(request.getParameter("roomamount").toString()));
				roomAllocation.setSDateOfStay(request.getParameter("date").toString());
				
				if(RoomDao.getRoomIdFromTransactionId(transactionId) == 0)
				{
					RoomDao.allocateRoom(roomAllocation);
					
				}
				else
				{
					session.setAttribute("Error", "Patient Already Has Room Booked");
					return false;
					
				}
			}
		}
		
		if(request.getParameter("domroomId")!=null )
		{
			if(request.getParameter("domId").toString().length()!=0 )
			{
				String domId=request.getParameter("domId").toString();
				String roomId=request.getParameter("domroomId").toString();
				String domIds[]=domId.split(",");
				String roomIds[]=roomId.split(",");
				
				for(int i=0;i<domIds.length;i++)
				{
					com.bss.framework.FrontController.trace("domId:"+domIds[i]);
					RoomAllocation roomAllocation=new RoomAllocation();
					roomAllocation.setITransactionId(transactionId);
					roomAllocation.setIDomId(Integer.parseInt(domIds[i]));
					roomAllocation.setIRoomId(Integer.parseInt(roomIds[i]));
					roomAllocation.setINoOfDoms(domIds.length);
					roomAllocation.setIAmount(Integer.parseInt(request.getParameter("domamount").toString()));
					roomAllocation.setSDateOfStay(request.getParameter("date").toString());
					RoomDao.allocateDom(roomAllocation);
					
				}
			}
			
		}
		
		
		
		
		return true;
	}
	
	public static ArrayList<RoomAllocation> allocatedRoomsDoms(int transactionId) throws Exception
	{
		return RoomDao.allocatedRoomsDoms(transactionId);
	}
	
	private boolean deallocateRoomDom(HttpServletRequest request, HttpSession session) throws Exception
	{
		int patientId=Integer.parseInt(request.getParameter("patientId").toString());
		String date=request.getParameter("date").toString();
		int transactionId=CheckInOutCommand.findLastTransactionId(patientId);
		RoomDao.deallocateAllRooms(transactionId, date);
		return true;
	}
	
	public static int getPatientIdFromRoomId(int roomId) throws Exception
	{
		return CheckInOutDao.findPatientId(RoomDao.getTransactionIdFromRoomId(roomId));
	}
	
	// Anand: domId is same as transactionid
	public static int getPatientIdFromDomId(int domId) throws Exception
	{
		return CheckInOutDao.findPatientId(RoomDao.getTransactionIdFromDomId(domId));
	}
	
	private boolean temporaryCheckOut(HttpServletRequest request, HttpSession session) throws Exception
	{
		int transactionId = CheckInOutCommand.findLastTransactionId(Integer.parseInt(request.getParameter("patientId").toString()));
		ArrayList<RoomAllocation> doms = RoomCommand.allocatedRoomsDoms(transactionId);
		
		for(int i = 0;i < doms.size(); i++)
		{
			if(doms.get(i).getIDomId() > 0)
			{
				RoomDao.temporaryCheckOut(transactionId, doms.get(i).getIDomId());
			}
		}
		
		return true;
	}
	
	
	private boolean temporaryCheckIn(HttpServletRequest request, HttpSession session) throws Exception
	{
		int transactionId = CheckInOutCommand.findLastTransactionId(Integer.parseInt(request.getParameter("patientId").toString()));
		
			return (RoomDao.tempCheckIn(transactionId));
		
		
	}
	
	
	public static ArrayList<Integer> getTempCheckOutDoms(int transactionId) throws Exception
	{
		return RoomDao.getTempCheckOutDoms(transactionId);
	}
	
	public static ArrayList<Integer> getTempCheckOutDoms() throws Exception
	{
		return RoomDao.getTempCheckOutDoms();
	}
	
	public static int getRoomIdFromTransactionId(int transactionId) throws Exception
	{
		return RoomDao.getRoomIdFromTransactionId(transactionId);
	}
	
	public static int findRoomAmount(int transactionId)throws Exception
	{
		return RoomDao.findRoomAmount(transactionId);
	}
	
	public static int findDomAmount(int transactionId)throws Exception
	{
		return RoomDao.findDomAmount(transactionId);
	}
}

